//
//  Helps.m
//  Login
//
//  Created by mac on 17/3/1.
//  Copyright © 2017年 ZF. All rights reserved.
//

#import "Helps.h"


@implementation Helps
+ (AppDelegate *)app
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

+ (MBProgressHUD *)showWaitHUDTips:(NSString *)tips yOffset:(CGFloat)y
{
    UIViewController *con = [self getCurrentViewController];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:con.view animated:YES];
    hud.yOffset = y;
    hud.labelText = tips;
    hud.labelFont = [UIFont systemFontOfSize:13];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

+ (void)showTips:(NSString *)tips forTime:(NSTimeInterval)time inView:(UIView *)view yOffset:(CGFloat)y
{
    [MBProgressHUD hideAllHUDsForView:view animated:YES];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = tips;
    hud.yOffset = y;
    hud.labelFont = [UIFont systemFontOfSize:13];
    [hud hide:YES afterDelay:time];
}

+ (void)showTips:(NSString *)tips forTime:(NSTimeInterval)time yOffset:(CGFloat)y
{
    UIViewController *con = [self getCurrentViewController];
    [MBProgressHUD hideAllHUDsForView:con.view animated:YES];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:con.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = tips;
    hud.yOffset = y;
    hud.labelFont = [UIFont systemFontOfSize:13];
    [hud hide:YES afterDelay:time];
}

+ (UIWindow *)window
{
    AppDelegate *app = [self app];
    return app.window;
}

+ (void)setNaviControllerRootView
{
    UIViewController *con;
    UITabBarController *tab = [Helps getTabCon];
    con = tab.viewControllers[tab.selectedIndex];
    if([con isKindOfClass:UINavigationController.class])
    {
        UINavigationController *nav = (UINavigationController *)con;
        con = nav.viewControllers.firstObject;
        [nav setViewControllers:@[con]];
    }
}

+ (UITabBarController *)getTabCon
{
    AppDelegate *app = [self app];
    UIViewController *con = app.window.rootViewController;
    return (UITabBarController *)con;
}

+ (UIViewController *)getCurrentViewController
{
    
    AppDelegate *app = [self app];
    UIViewController *con = app.window.rootViewController;
    while (YES) {
        if ([con isKindOfClass:UINavigationController.class]) {
            UINavigationController *nav = (UINavigationController *)con;
            con = nav.viewControllers.lastObject;
        }else if ([con isKindOfClass:UITabBarController.class]) {
            UITabBarController *tab = (UITabBarController *)con;
            con = tab.viewControllers[tab.selectedIndex];
        }else if (con.presentedViewController) {
            con = con.presentedViewController;
        }else{
            break;
        }
    }
    
    return con;
}

@end
